package com.jdym.back.controller;


import com.jdym.R;
import com.jdym.back.entity.CoBank;
import com.jdym.back.service.CoBankService;
import com.jdym.utils.UploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 * 合作银行表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-09-20
 */
@RestController
@CrossOrigin
@RequestMapping("/back/coBank")
public class CoBankController {

    @Autowired
    CoBankService coBankService;

//    上传图片
@RequestMapping("/imageCobangk")
@ResponseBody
public R image(@RequestParam MultipartFile file){
    //调用工具类完成文件上传
    System.out.println("file:"+file);
    String imagePath = UploadUtils.upload(file);
    System.out.println(imagePath);
    String url="http://localhost:8010"+imagePath;
    if(imagePath!=null){
        return R.ok().data("img",url);
    }else {
        return R.ok().data("img","上传失败");
    }

}

    @RequestMapping(value = "/getAllCobank")
    public R getAllCobank(){

        List<CoBank> coBanks = coBankService.getBaseMapper().selectList(null);
        return R.ok().data("cobanks",coBanks);
    }
//根据id查找
    @RequestMapping(value = "/getCobankById/{coBankId}")
    public R getCobankById(@PathVariable int coBankId )
    {
       CoBank coBank= coBankService.getBaseMapper().selectById(coBankId);
       return R.ok().data("coBank",coBank);
    }
//    更新
    @PostMapping(value = "/updateBank")
//    int cobankId, String cobankName, String cobankNo, String cobankTel, BigDecimal cobankRate,String cobankEmail,String cobankAdderss
    public R updateBank(@RequestParam(required = false) Integer cobankId,@RequestParam(required = false) String cobankImg, @RequestParam(required = false) String cobankName,@RequestParam(required = false) String cobankNo,@RequestParam(required = false) String cobankTel,@RequestParam(required = false) BigDecimal cobankRate,@RequestParam(required = false) String cobankEmail,@RequestParam(required = false) String cobankAdderss){

        System.out.println("form:"+cobankId+cobankAdderss+cobankName+cobankImg);
        CoBank coBank=new CoBank();
        if(cobankId!=null){
            coBank.setCobankId(cobankId);
        }
        if(cobankName!=null){
            coBank.setCobankName(cobankName);
        }
        if(cobankNo!=null){
            coBank.setCobankNo(cobankNo);
        }
        if(cobankTel!=null){
            coBank.setCobankTel(cobankTel);
        }
        if(cobankRate!=null){
            coBank.setCobankRate(cobankRate);
        }
        if(cobankEmail!=null){
            coBank.setCobankEmail(cobankEmail);
        }
        if(cobankAdderss!=null){
            coBank.setCobankAdderss(cobankAdderss);
        }
        if(cobankImg!=null){
            coBank.setCobankImg(cobankImg);
        }
        System.out.println("新增的银行数据："+coBank);
        if(coBank.getCobankId()==null){
            coBankService.insertCoBank(coBank);
        }else {
            coBankService.getBaseMapper().updateById(coBank);
        }
        return R.ok();
    }

    @DeleteMapping("/delCobank/{bankId}")
    public R delCobank(@PathVariable int bankId){
        coBankService.getBaseMapper().deleteById(bankId);
        return R.ok();
    }


    //////////////////////////////////////////张

    //    查询根据(关键字)
    @GetMapping("/selectCobank/{cobankId}")
    public R select(@PathVariable  int cobankId){
        List<CoBank> coBankList = coBankService.select(cobankId);
        System.out.println("============"+coBankList);
        return R.ok().data("coBankList",coBankList).message("连接成功") ;
    }

    //    根据ID值定位查询
    @GetMapping("/selectCobankByID/{id}")
    public R selectByID(@PathVariable int  id){
        CoBank coBanks = coBankService.selectByID(id);
        System.out.println("=====11111======="+coBanks);
        return R.ok().data("coBanks",coBanks).message("连接成功") ;
    }


    //修改保存
    @PostMapping("/updateCobank/{id}/{name}/{no}/{rate}/{tel}/{email}/{address}")
    public R updateByID(@PathVariable int id,
                        @PathVariable String name,
                        @PathVariable String no,
                        @PathVariable String tel,
                        @PathVariable BigDecimal rate,
                        @PathVariable String email,
                        @PathVariable String address){
        CoBank coBank = new CoBank();
        coBank.setCobankId(id);
        coBank.setCobankName(name);
        coBank.setCobankNo(no);
        coBank.setCobankTel(tel);
        coBank.setCobankRate(rate);
        coBank.setCobankEmail(email);
        coBank.setCobankAdderss(address);
        coBankService.getBaseMapper().updateById(coBank);

//        CoBank coBankupdateID = coBankService.updateByID(id);

        System.out.println("===修改保存===="+coBank);
//        return R.ok().data("coBankupdateID",coBankupdateID).message("修改成功") ;

        return  R.ok().data("msg","修改成功");
    }


}

